package com.sofwin.controller;/**
 * @PackageName:com.sofwin.controller
 * @author:andyLiu
 * @DateTime:2020/12/23 16:30
 * @Description:[TODO]输入当前模块作用
 **/

import com.google.code.kaptcha.Constants;
import com.sofwin.pojo.SUser;
import com.sofwin.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @ClassName: SystemController
 * @Description: TODO
 * @Author: andyliu
 * @Date: 2020/12/23 16:30
 * @Version: 1.0
 **/
@Controller
@RequestMapping("/system")
public class SystemController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String toLogin(String msg,Model model){
        model.addAttribute("msg",msg);
        return "login";
    }

    @PostMapping("/checkLogin")
    public String checkLogin(SUser user, HttpSession session,String code){
        String mcode = session.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString();
        if(!mcode.equalsIgnoreCase(code)){
            return "redirect:login?msg=-1";
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginAccount(),user.getPwd());
        try {
            // 认证（shiro)
            subject.login(token);
        }catch (UnknownAccountException ex){
            // 通过捕获的异常来判定当前认证失败的原因的
            // 用户名不存在
            return "redirect:login?msg=1";

        }catch (IncorrectCredentialsException ex){
            // 密码错误
            return "redirect:login?msg=2";
        }catch (LockedAccountException ex){
            // 用户被锁定
            return "redirect:login?msg=3";
        }
        user =  userService.selectUserByLoginAccount(user);
        session.setAttribute("user",user);
        return "redirect:index";
    }


    public String checkLogin2(SUser user, HttpSession session,String code){
        String mcode = session.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString();
        if(!mcode.equalsIgnoreCase(code)){
            return "redirect:login";
        }
        // 验证用户登录
        boolean status = userService.checkLogin(user);
        if(status){
            // 查询当前用户对应的橘色
            user =  userService.selectUserByLoginAccount(user);
            session.setAttribute("user",user);
            // 将角色id放入到session中
            // 验证成功
            return "redirect:index";
        }else{
            // 验证失败
            return "redirect:login";
        }
    }

    @RequestMapping("index")
    public String toIndex(){
        return "index";
    }
    @RequestMapping("logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();// 退出 会清空授权信息
        return "redirect:login";
    }
}
